package com.yunxiang.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class AuctionController extends Controller {

	public void getAuctionList(){
		String sql = "select a.id,a.location,a.end_time,a.qty,cp.name as company_name from m_auction a  " +
				"left join m_company cp on a.company_id = cp.id " +
				"where a.status = 0 order by a.create_date desc";
		List<Record> list = Db.find(sql);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		JSONArray jArray = new JSONArray();
		for (Record record : list) {
			JSONObject jObject = new JSONObject();
			jObject.put("id", record.get("id"));
			jObject.put("location", record.get("location"));
			jObject.put("end_time", sdf.format(record.getDate("end_time")));
			jObject.put("qty", record.get("qty"));
			jObject.put("company_name", record.get("company_name"));
			jArray.add(jObject);
		}
		renderJson(jArray.toString());
	}
}
